package com.lanou3g.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou3g.bean.Flats;
import com.lanou3g.mapper.FlatsMapper;
import com.lanou3g.service.FlatsService;
import com.lanou3g.util.Result;
@Service
public class FlatsServiceImpl implements FlatsService {

	@Autowired
	private FlatsMapper flatsMapper;
	
	@Override
	public Result getFlats(Integer page, Integer limit) {
		List<Flats> flatsList = flatsMapper.getFlats();
		PageHelper.startPage(page, limit);
		PageInfo<Flats> pageInfo = new PageInfo<>(flatsList);
		Result result = new Result(0,"获取公寓列表成功!");
		result.setCount(pageInfo.getTotal());
		result.setData(pageInfo.getList());
		return result;
	}

	@Override
	public Result getFlatsByName(String flatsName) {
		Flats flats = flatsMapper.getFlatsByName(flatsName);
		if(flats == null) {
			//没有查到这个公寓
			return new Result(0,"没有这个公寓");
		}else {
			return new Result(1,"这个公寓已经存在");
		}
	}

	@Override
	public Result insertFlats(Flats flats) {
		int rows = flatsMapper.insertFlats(flats);
		if(rows == 0) {
			//说明添加失败
			return new Result(1,"添加公寓失败!");
		}else {
			return new Result(0,"添加公寓成功!");
		}
	}

	@Override
	public List<Flats> getFlats() {
		return flatsMapper.getFlats();
	}

	@Override
	public Flats getFlatsById(Integer id) {
		return flatsMapper.getFlatsById(id);
	}

	@Override
	public Result updateFlats(Flats flats) {
		int rows = flatsMapper.updateFlats(flats);
		if(rows == 0) {
			//更新失败
			return new Result(1,"更新公寓信息失败!");
		}else {
			return new Result(0,"更新公寓信息成功!");
		}
	}

	@Override
	public Result deleteFlatsById(Integer id) {
		int rows = flatsMapper.deleteFlatsById(id);
		if(rows == 0) {
			//删除失败
			return new Result(1,"删除公寓失败!");
		}else {
			return new Result(0,"删除公寓成功!");
		}
	}

}
